package com.mogujie.tt.ui.adapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.ContentValues;
import android.content.Context;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.mogujie.tt.DB.entity.UserEntity;
import com.mogujie.tt.R;
import com.mogujie.tt.imservice.manager.IMContactManager;
import com.mogujie.tt.imservice.manager.IMNewFriendsManager;
import com.mogujie.tt.ui.widget.IMBaseImageView;
import com.mogujie.tt.utils.IMUIHelper;


public class NewFriendsAdapter extends BaseAdapter  implements
        AdapterView.OnItemClickListener,
        AdapterView.OnItemLongClickListener{
    Context context;

 private ArrayList<UserEntity> userList = new ArrayList<>();//请求添加好友的用户


    //构造方法
    public NewFriendsAdapter(Context context,ArrayList<UserEntity> pUserList) {
        this.context = context;
        userList =pUserList;

    }

    public void putUserList(ArrayList<UserEntity> pUserList){
        this.userList.clear();
        if(pUserList == null){
            return;
        }
        this.userList = pUserList;
        notifyDataSetChanged();//更新数据，只有在UI线程执行时
    }

    @Override
    public int getCount() {
        return userList.size();
    }

    @Override
    public UserEntity getItem(int position) {
        return userList.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView( int position, View convertView, ViewGroup parent) {

         ViewHolder holder = null;
        final UserEntity  userEntity= (UserEntity)getItem(position);
        if(userEntity == null){
            // todo 这个会报错误的，怎么处理
            return null;
        }
        String reason = "请求添加你为好友";

        if(convertView == null){
            holder =new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_newfriendsmsag, null);
            holder.iv_avatar = (IMBaseImageView) convertView.findViewById(R.id.iv_avatar);//个性化头像
            holder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);//昵称
            holder.tv_reason = (TextView) convertView.findViewById(R.id.tv_reason);//请求信息
            holder.btn_add = (Button) convertView.findViewById(R.id.btn_add);//添加好友按钮
            holder.tv_added =(TextView)convertView.findViewById(R.id.tv_added);//等待验证
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }

        holder.tv_name.setText(userEntity.getMainName().toString());//设置昵称

        holder.iv_avatar.setDefaultImageRes(R.drawable.tt_default_user_portrait_corner);
        holder.iv_avatar.setCorner(15);
        holder.iv_avatar.setImageResource(R.drawable.tt_default_user_portrait_corner);
        holder.iv_avatar.setImageUrl(userEntity.getAvatar());//设置头像

        if(userEntity.getRemark().equals("")) {//设置请求信息
            holder.tv_reason.setText(reason);
        }else {
            holder.tv_reason.setText(userEntity.getRemark().toString());
        }

        if(userEntity.getRelation() == 2){
            holder.btn_add.setVisibility(View.VISIBLE);
            holder.tv_added.setVisibility(View.GONE);
            final ViewHolder finalHolder = holder;
            holder.btn_add.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    IMNewFriendsManager.instance().reqGetAddFriend(finalHolder.tv_reason.getText().toString().trim(), userEntity.getPeerId());
             //       Toast.makeText(context, "同意添加好友", Toast.LENGTH_SHORT).show();
                    IMNewFriendsManager.instance().agreeFriendsAdd(userEntity.getPeerId());
                }
            });
        }else if(userEntity.getRelation() == 1) {
            holder.btn_add.setVisibility(View.GONE);
            holder.tv_added.setVisibility(View.VISIBLE);
            holder.tv_reason.setText("对方正在验证你的信息");
        }

        return convertView;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Object object =  getItem(position);
        if(object instanceof UserEntity){
            UserEntity userEntity = (UserEntity) object;
            IMUIHelper.openAddUserProfileActivity(context, userEntity.getPeerId(),1);
        }
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        return false;
    }

    private static class ViewHolder {
        IMBaseImageView iv_avatar;//头像
        TextView tv_name;//昵称
        TextView tv_reason;//请求信息
        Button btn_add;//添加好友按钮
        TextView tv_added;//等待验证

    }

}
